<?php
/**
 * View for displaying a user's profile
 * @author Christophe Sourisse, Dominik Imsirovic
 */
?>

<div id="left"></div>
<br /><br />


<div id="view-content">
	<h1>Welcome to the <span class="logo">What's up?</span> page of <?=$this->label." ".$this->topic["userName"] ?></h1>
   <div id="user-infos">
       <?php if($this->isTopic==0){
           
           
        echo "<img src=\"..\images\upload/".$this->topic["picture"]."\" alt=\"no picture\" width=\"80\" height=\"80\"/>";
       
      echo "<p>";
      echo  "<b>Name in real life:</b>  ".$this->topic["name"]."<br>";
      echo  "<b>Current location:</b>  ".$this->topic["location"]."<br>";
      echo "<b>Born on:</b>  ".$this->topic["birthdate"]."<br><br>";
               
               }
               else{
      echo "<p>";
      echo  "<b>Author:</b>  ".$this->topic["name"]."<br><br>";
               }
               ?>
      <?= $this->topic["description"] ?>
       </p>
   </div>
   
        <?php
if (isset($this->forms)) {
   foreach($this->forms as $form)
      echo $form->render();
} ?> 

        <div id="profileMenu">
          
            <?php 
          if($this->currentUser==1)  
             echo "<a href=\".user?avatar=".$this->topic["userName"].
                     "&data=news\">News</a>";
                    ?>
            <a href="user?avatar=<?=$this->topic["userName"]?>&data=timeline">Timeline</a>
             <a href="user?avatar=<?=$this->topic["userName"]?>&data=followers">Followers</a>
            <?php 
            if($this->isTopic==0){
          //echo  "<a href=\"user?avatar=".$this->topic["userName"]."&data=follows\">Follows</a>";
          echo  "<a href=\"user?avatar=".$this->topic["userName"]. "&data=topics\">Follows</a>";
            }
                    ?>
        </div>
        
    <?php 
    if(strcmp($this->data,"news")==0){
            echo "<h1>News from my topics</h1>";
        echo "<ul id=\"news\">";

if (count($this->postsFromTopics) > 0) {
    $i=1;
    $more=false;
   foreach ($this->postsFromTopics as $post) {
      
       /*echo "<li>";
      echo "<h6>Posted by ".$message["author"]." on ".$message["date"]."</h6>";
      echo $message["text"];
      echo "</li>"; */
      
      if($i<=$this->count){
      $poster = Model_Users::getUser($post["author"]);
      $topic=Model_Users::getUser($post["topic"]);
      
      //prevent XSS
      $poster["userName"]=Model_Users::preventXSS( $poster["userName"]);
      $post["text"]=Model_Users::preventXSS($post["text"]);
      
        $topicText=" on page <a href=\"user?avatar=".$topic["userName"]."&data=timeline\">".$topic["userName"]."</a>";
     
      echo "<div id=\"post\">";
      if(isset($poster[picture]))
      echo "<img src=\"..\images\upload/".$poster["picture"]."\" alt=\"no picture\" width=\"80\" height=\"80\"/>";
      echo "<div id=\"text\">";
      echo "<h6>Posted on ".$post["date"]." by <a href=\"user?avatar=".$poster["userName"]."&data=timeline\">".$poster["userName"]."</a>".$topicText."</h6>";
      echo $post["text"]."</div>";
      echo "</div>";
      
      $i++;
      }else if(!$more){
          echo "<a href=\"user?avatar=".$this->topic["userName"]."&data=".$this->data."&count=".($this->count+10)."\">Show more </a>";
          $more=true;
      }
   }
}
else {
            echo"<em>No news from your topics yet...</em>";
}
      echo   "</ul>";
    }
   
   else if(strcmp($this->data,"timeline")==0){
        
        echo "<h3>Timeline of ".$this->topic["userName"]."</h3>";
        echo "<div id=\"timeline\"> ";
      //Model_Profile::showMessages($this->postsFromVisitedUser,false);
      if (count($this->postsFromVisitedUser) > 0) {
          
          $i=1;
          $more=false;
          
   foreach ($this->postsFromVisitedUser as $post){
     
       //$post = $this->postsFromVisitedUser[$i];
      if($i<=$this->count){
      $poster = Model_Users::getUser($post["author"]);
    
        $topic="";
        
         //prevent XSS
      $poster["userName"]=Model_Users::preventXSS( $poster["userName"]);
      $post["text"]=Model_Users::preventXSS($post["text"]);
     
      echo "<div id=\"post\">";
      if(isset($poster["picture"]))
      echo "<img src=\"..\images\upload/".$poster["picture"]."\" alt=\"no picture\" width=\"80\" height=\"80\"/>";
      echo "<div id=\"text\">";
      echo "<h6>Posted on ".$post["date"]." by <a href=\"user?avatar=".$poster["userName"]."&data=timeline\">".$poster["userName"]."</a>".$topic."</h6>";
      echo $post["text"]."</div>";
      echo "</div>";
      
      $i=$i+1;
   }
   else if(!$more){
          echo "<a href=\"user?avatar=".$this->topic["userName"]."&data=".$this->data."&count=".($this->count+10)."\">Show more </a>";
          $more=true;
      }
   }
}
else {
         echo "<em>This user didn't post any message yet.</em>";
}
        echo "</div>";
      }
    
    else if(strcmp($this->data,"topics")==0){
       echo "<h3>Topics of".$this->topic["userName"]."</h3>";       
       echo "<ul id=\"topics\">";

if (count($this->topics) > 0) {
   foreach ($this->topics as $topic) {
      echo "<li>";
      echo "<a href=\"/WhatsUpProject/public/search/user?avatar=".
              $topic["userName"]."\">".Model_Users::preventXSS($topic["userName"])."</a>";
      echo "</li>";
   }
}
else {

     echo  "<em>This user didn't subscribe to any topic yet.</em>";
}

   echo "</ul>";
   }
     else  if(strcmp($this->data,"followers")==0){
        echo "<h3>".$this->topic["userName"]."'s followers</h3>";
        echo "<ul id=\"subscribers\">";

if (count($this->subscribers) > 0) {
   foreach ($this->subscribers as $subscriber) {
      echo "<li>";
      echo "<a href=\"/WhatsUpProject/public/search/user?avatar=".
              $subscriber["userName"]."&data=timeline\">".Model_Users::preventXSS($subscriber["userName"])."</a>";
      echo "</li>";
   }
}
else {

    echo  "<em>No one is following this user yet.</em>";
}
    echo "</ul>";
     }
   ?>
      
</div>

<?php
//Add hidden information about session that
//will be used by jquery code
?>
<div id="hidden">
       <p id="hiddenName"><?php echo $this->session->user["userName"]; ?></p>
       <p id="hiddenImage"> <?php echo $this->session->user["picture"]; ?> </p>
</div>